const Upyun = require('../../utils/upyun-wxapp-sdk')
const upyun = new Upyun({
  bucket: 'feijiu-img5',
  operator: 'feijiu',
  // getSignatureUrl: 'https://zsbpt.zaisubao.cn/index.php/Api/Feijiu/getSignature'
  getSignatureUrl: 'https://apiwf.feijiu.net/api/FeijiuApp/Feijiu_Get_UpySign'
})
let date = new Date;
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();  
const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
const second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
const milliSeconds = date.getMilliseconds();
const app =getApp()
function Appendzero(obj){
  if(obj<10) return "0" +""+ obj;
  else return obj;
}
function threeRand () {
var rand = "";
for(var i = 0; i < 3; i++){
    var r = Math.floor(Math.random() * 10);
    rand += r;
}
return rand;
}
function formatTime (seconds){
  let hour = Math.floor(seconds / 3600) >= 10 ? Math.floor(seconds / 3600) : '0' + Math.floor(seconds / 3600);
  seconds -= 3600 * hour;
  let min = Math.floor(seconds / 60) >= 10 ? Math.floor(seconds / 60) : '0' + Math.floor(seconds / 60);
  seconds -= 60 * min;
  let sec = seconds >= 10 ? seconds : '0' + seconds;
  return min + ':' + sec;
}
function choosevod(tempFilePath,r,cb) {
  var that = this;
  console.log(tempFilePath)
  var index = tempFilePath.lastIndexOf("\.");
  var kuozhan = tempFilePath.substring(index + 1, tempFilePath.length);
  var name = Appendzero(day) + '' + hour + '' + minute + '' + second + '' + milliSeconds + '' + threeRand() + '.' + kuozhan;
  var vodSrc = tempFilePath;
  wx.showLoading({
    title: "视频正在上传中"
  })
  upyun.upload({
    localPath: vodSrc,
    signature:r.sign,
    policy:r.policy,
    remotePath: '/video/product/' + year + '/' + Appendzero(month) + '/' + Appendzero(day) + '/' + name,
    success: function (res) {
      console.log(res,'上传视频')
      // return
      wx.showToast({
        title: '上传成功',
        icon: 'success',
        duration: 1000
      })
      if (res.statusCode == 200) {
        res.data = JSON.parse(res.data);
        if (res.data.code == 200) {
          // that.setData({
          //   vodurl: res.data.url
          // })
          return typeof cb == "function" && cb(res.data)

        } else {
          app.tishi('视频上传失败,请重试!')
        }

      } else {
        app.tishi('视频上传失败,请重试!')
      }
    },
    fail: function ({
      errMsg
    }) {
      console.log('uploadImage fail, errMsg is', errMsg)
    },
  })
}

//三张图片合拼成,分开的字符串
function picJoin (arr) {
  var pic = '';
  for (var i = 0; i < arr.length; i++) {
    if ( i ==arr.length-1 ) {
      pic+= arr[i][0];
    }else{
      pic+= arr[i][0]+'|';
    }        
  }
  return pic;
}
module.exports = {
  choosevod,
  picJoin
}